﻿using Scap.Data;
using Scap.Info;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scap.Core
{
    public class DeviceTaskHandlerDownload : DeviceTaskHandler
    {
        private int iMachineNumber = 1;



        public DeviceTaskHandlerDownload(DeviceTaskModel model)
            : base(model)
        {

        }
    


        public override void Run()
        {
            ScapDevice dev = _model.Device;

            _logger.Info("{0} | Downloading assistance info...", dev.IpAddress);

            if (dev.Status == DeviceStatus.Offline)
            {
                _logger.Info("{0} | Downloading assistance info...[FAILED]: device offline", dev.IpAddress);
                return;
            }

            string sdwEnrollNumber = string.Empty;
            int idwVerifyMode = 0;
            int idwInOutMode = 0;
            int idwYear = 0;
            int idwMonth = 0;
            int idwDay = 0;
            int idwHour = 0;
            int idwMinute = 0;
            int idwSecond = 0;
            int idwWorkcode = 0;
            int idwErrorCode = 0;

            //disable the device
            _zkDevice.EnableDevice(iMachineNumber, false);

            //read all the attendance records to the memory
            if (_zkDevice.ReadGeneralLogData(iMachineNumber))
            {
                ScapData db = new ScapData();
                ScapEmployeeAssistanceCollection coll = new ScapEmployeeAssistanceCollection();

                //get records from the memory
                while (_zkDevice.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out idwVerifyMode,
                           out idwInOutMode, out idwYear, out idwMonth, out idwDay, 
                           out idwHour, out idwMinute, out idwSecond, ref idwWorkcode))
                {
                    // save it
                    ScapEmployeeAssistance record = new ScapEmployeeAssistance();
                    record.CheckTime = new DateTime(idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond);
                    record.DeviceId = dev.Id;
                    record.EmployeeId = int.Parse(sdwEnrollNumber);
                    record.WorkCode = idwWorkcode;
                    record.Type = (ScapAssistenceType)idwInOutMode;

                    coll.Add(record);
                    db.Entry(record).State = System.Data.EntityState.Added;
                }

                db.SaveChanges();
                // clear assistance
                _zkDevice.ClearGLog(iMachineNumber);
            }
            else
            {
                _zkDevice.GetLastError(ref idwErrorCode);

                if (idwErrorCode != 0)
                {
                    _logger.Error("{0} | Reading data from terminal failed,ErrorCode: " + idwErrorCode.ToString(), dev.IpAddress);
                }
                else
                {
                    _logger.Error("{0} | No data from terminal returns!", dev.IpAddress);
                }
            }

            //enable the device
            _zkDevice.EnableDevice(iMachineNumber, true);
            _logger.Info("{0} | Downloading assistance info...[OK]", dev.IpAddress);
        }
    }
}
